package com.gateway.plugin.resilience4j.executor;

import com.gateway.plugin.api.result.GatewayResultEnum;
import com.gateway.plugin.api.result.GatewayResultWrap;
import com.gateway.plugin.api.util.WebFluxResultUtils;
import com.gateway.plugin.base.fallback.FallbackHandler;
import com.gateway.plugin.resilience4j.Resilience4JPlugin;
import com.gateway.plugin.resilience4j.config.Resilience4JConfig;
import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import org.springframework.http.HttpStatus;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeoutException;
import java.util.function.Function;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/24 09:43
 **/
public interface Executor extends FallbackHandler {

    /**
     * resilience run.
     *
     * @param toRun    the toRun
     * @param fallback the fallback
     * @param conf     the conf
     * @param <T>      the t
     * @return mono
     */
    <T> Mono<T> run(Mono<T> toRun, Function<Throwable, Mono<T>> fallback, Resilience4JConfig conf);


    /**
     * do fallback with not fallback method.
     *
     * @param exchange  the exchange
     * @param throwable the throwable
     * @return Mono
     */
    @Override
    default Mono<Void> withoutFallback(ServerWebExchange exchange, Throwable throwable) {
        Object error;
        if (throwable instanceof TimeoutException) {
            exchange.getResponse().setStatusCode(HttpStatus.GATEWAY_TIMEOUT);
            error = GatewayResultWrap.error(exchange, GatewayResultEnum.SERVICE_TIMEOUT);
        } else if (throwable instanceof Resilience4JPlugin.CircuitBreakerStatusCodeException) {
            return Mono.error(throwable);
        } else if (throwable instanceof CallNotPermittedException) {
            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            error = GatewayResultWrap.error(exchange, GatewayResultEnum.SERVICE_RESULT_ERROR);
        } else if (throwable instanceof RequestNotPermitted) {
            exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS);
            error = GatewayResultWrap.error(exchange, GatewayResultEnum.TOO_MANY_REQUESTS);
        } else {
            exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
            error = GatewayResultWrap.error(exchange, GatewayResultEnum.SERVICE_RESULT_ERROR);
        }
        return WebFluxResultUtils.result(exchange, error);
    }

    /**
     * default error.
     *
     * @param exchange the exchange
     * @return Mono
     */
    default Mono<Void> error(ServerWebExchange exchange) {
        exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
        Object error = GatewayResultWrap.error(exchange, GatewayResultEnum.SERVICE_RESULT_ERROR);
        return WebFluxResultUtils.result(exchange, error);
    }
}
